package com.ease.integration.telent;

import com.ease.common.util.Detect;

public abstract class TelnetClient extends Detect {

	public static final int BUFFER_LENGTH = 1024;

	public static final String CRLF = "\r";// (回车返回换行)
	// 控制字符：5个
	// \' 单引号字符
	// \\ 反斜杠字符
	// \r 回车
	// \n 换行
	// \f 走纸换页
	// \t 横向跳格
	// \b 退格

	protected String[] commandPrompt = { ">", "#" };

	protected String morePrompt = "More";

	protected String host;

	protected int port;

	protected String usernamePrompt = "Username:";

	protected String username;

	protected String passwordPrompt = "Password:";

	protected String password;

	public abstract boolean connect() throws RuntimeException;

	public abstract boolean login() throws RuntimeException;

	public abstract boolean disconnect() throws RuntimeException;

	// final String prompt,
	// public abstract String send(String command) throws RuntimeException;
	public abstract void request(String command) throws RuntimeException;

	public abstract String response(String prompt);

	// public String send(String command) throws RuntimeException {
	//
	// response(usernamePrompt);
	// request(username);
	// response(passwordPrompt);
	// request(password);
	//
	//
	// request(command);
	// return response(null);
	//
	// }

	public static String appendEnterIfNecessary(String command) {
		if (null != command) {
			if (!command.endsWith(CRLF)) {
				return command + CRLF;
			}
		}
		return command;
	}

	/**
	 * @return the usernamePrompt
	 */
	public String getUsernamePrompt() {
		return usernamePrompt;
	}

	/**
	 * @param usernamePrompt
	 *            the usernamePrompt to set
	 */
	public void setUsernamePrompt(String usernamePrompt) {
		this.usernamePrompt = usernamePrompt;
	}

	/**
	 * @return the passwordPrompt
	 */
	public String getPasswordPrompt() {
		return passwordPrompt;
	}

	/**
	 * @param passwordPrompt
	 *            the passwordPrompt to set
	 */
	public void setPasswordPrompt(String passwordPrompt) {
		this.passwordPrompt = passwordPrompt;
	}

	/**
	 * @return the host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * @param host
	 *            the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the morePrompt
	 */
	public String getMorePrompt() {
		return morePrompt;
	}

	/**
	 * @param morePrompt
	 *            the morePrompt to set
	 */
	public void setMorePrompt(String morePrompt) {
		this.morePrompt = morePrompt;
	}

	/**
	 * @return the commandPrompt
	 */
	public String[] getCommandPrompt() {
		return commandPrompt;
	}

	/**
	 * @param commandPrompt
	 *            the commandPrompt to set
	 */
	public void setCommandPrompt(String[] commandPrompt) {
		this.commandPrompt = commandPrompt;
	}

}
